<?php
class App_Plugin_FlashMessages extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $messenger = new Lib_Flash;
        $messages = $messenger->getMessages(Lib_Flash::PRE);

        $messenger->clearMessages(Lib_Flash::PRE);

        $layout = Zend_Layout::getMvcInstance();

        $view = $layout->getView();
        $view->messages = $messages;
    }

    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $messenger = new Lib_Flash();
        $messages = $messenger->getMessages(Lib_Flash::POST);
        $messenger->getMessages(Lib_Flash::POST);
        $messenger->clearMessages(Lib_Flash::POST);

        $layout = Zend_Layout::getMvcInstance();

        $view = $layout->getView();
        $pre_messages = $view->messages;
        if (is_array($pre_messages)) {
            $view->messages = array_merge_recursive($pre_messages, $messages);
        } else {
            $view->messages = $messages;
        }
    }
}
